package cosmos.dubbo.cunsumer.rest;

import cosmos.dubbo.cunsumer.service.TestService;
import cosmos.dubbo.domain.User;
import cosmos.dubbo.dto.UserDto;
import io.swagger.annotations.*;
import lodsve.core.context.ApplicationHelper;
import lodsve.web.springfox.properties.SpringFoxProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * .
 *
 * @author sunhao(sunhao.java@gmail.com)
 * @version V1.0, 16/1/18 下午6:42
 */
@Api(value = "Mybatis的CRUD测试", description = "Mybatis的CRUD测试")
@RestController
@RequestMapping("/user")
public class TestController {
    @Autowired
    private TestService testService;

    @ApiOperation(value = "获取用户", notes = "获取用户")
    @ApiResponses({@ApiResponse(code = 200, message = "获取用户成功")})
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User loadUser(@ApiParam(name = "id", value = "用户ID") @PathVariable("id") Long id) {
        ApplicationHelper.getInstance().getBean(SpringFoxProperties.class);
        return testService.loadUser(id);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiResponses({@ApiResponse(code = 200, message = "删除用户成功")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Long delete(@ApiParam(name = "id", value = "用户ID") @RequestParam("id") Long id) {
        return testService.delete(id);
    }

    @ApiOperation(value = "更新用户", notes = "更新用户")
    @ApiResponses({@ApiResponse(code = 200, message = "更新用户成功")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Long update(@ApiParam(name = "userDto", value = "需要更新的用户") @RequestBody UserDto userDto) {
        return testService.update(userDto);
    }

    @ApiOperation(value = "保存用户", notes = "保存审核用户")
    @ApiResponses({@ApiResponse(code = 200, message = "审核用户成功")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Long save(@ApiParam(name = "userDto", value = "需要保存的用户") @RequestBody UserDto userDto) {
        return testService.save(userDto);
    }

    @ApiOperation(value = "列出用户", notes = "列出用户")
    @ApiResponses({@ApiResponse(code = 200, message = "列出用户成功")})
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Page<User> findAll(@ApiParam(name = "pageable", value = "分页信息,传参方式：?page=0&size=10") @PageableDefault(page = 0, size = 10) Pageable pageable) {
        return testService.list(pageable);
    }
}
